/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.syntax;

import java.util.LinkedList;
import java.util.List;

public class StatementParts 
{
	LinkedList<StatementPart> parts = new LinkedList<StatementPart>();
	
	StringBuffer sbWord = null ;
	StringBuffer sbNotWord = null ;
	
	private void trace(String m) {
		//System.out.println(m);
	}
	
	public boolean currentWord() {
		return sbWord != null ;
	}

	public void startNewWord(char c) {
		trace("startNewWord("+c+")");

		//--- End of "not word"
		if ( sbNotWord != null ) {
			parts.add( new StatementPart(sbNotWord.toString() , StatementPart.NOT_WORD) );
			sbNotWord = null ;
		}

		//--- Beginning of "word"
		sbWord = new StringBuffer();
		sbWord.append(c);
	}
	
	public void addToWord(char c) {
		trace("addToWord("+c+")");
		sbWord.append(c);
	}
	
	public void endOfCurrentWord(char c) {
		trace("endOfCurrentWord("+c+")");

		//--- End of "word"
		parts.add( new StatementPart(sbWord.toString(), StatementPart.WORD) );
		sbWord = null ;
		
		//--- Beginning of "not word"
		sbNotWord = new StringBuffer();
		sbNotWord.append(c);
	}

	public void noCurrentWord(char c) {
		trace("noCurrentWord("+c+")");
		if ( null == sbNotWord ) {
			sbNotWord = new StringBuffer();
		}
		sbNotWord.append(c);
	}
	
	public void close() {
		if ( sbNotWord != null ) {
			parts.add( new StatementPart(sbNotWord.toString(), StatementPart.NOT_WORD) );
			sbNotWord = null ;
		}
		if ( sbWord != null ) {
			parts.add( new StatementPart(sbWord.toString(), StatementPart.WORD) );
			sbWord = null ;
		}
	}

	public List<StatementPart> getList() {
		return parts;
	}
	
}
